package com.crm.controller;

import cn.hutool.core.util.ObjectUtil;
import com.crm.entity.CompanyEntity;
import com.crm.entity.Director;
import com.crm.service.DirectorService;
import com.crm.service.EntityService;
import com.crm.util.RunnablePlaywright;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author zhangzhen
 * @version 1.0
 */
@RestController
@RequestMapping("/test/")
@Slf4j
@EnableAsync
public class TestController {

    @Autowired
    private RunnablePlaywright runnablePlaywright;

    @Autowired
    private EntityService entityService;

    @Autowired
    private DirectorService directorService;

    @GetMapping("/test")
    public void test() {
        int end, start = 0;
        for (int i = 1; i < 9; i++) {
            end = start + 10000;
            System.out.println(start + ":" + end);
            runnablePlaywright.startTask(start, end);
            start = end;
        }
    }

    @GetMapping("/testStart")
    public void testPageSiz() {

//        SELECT business_id  FROM `entity` a where  a.business_id<00020000 order by business_id DESC LIMIT 1;
//
//        SELECT business_id  FROM `entity` a where  a.business_id<00050000 order by business_id DESC LIMIT 1;
//
//        SELECT business_id  FROM `entity` a where  a.business_id<00080000 order by business_id DESC LIMIT 1;
        //00010047
        // runnablePlaywright.startTask(10047, 20000);
//        runnablePlaywright.startTask(40029, 50000);
//        runnablePlaywright.startTask(70024, 80000);
        int start = 70024;
        int end = 80000;
        List<Integer> need = new ArrayList<>();
        for (int i = start; i <= end; i++) {
            need.add(i);
        }
        int batchSize = 400;
        int totalSize = need.size();
        int batchCount = (totalSize + batchSize - 1) / batchSize; // 计算需要的批次
        for (int i = 0; i < batchCount; i++) {
            int fromIndex = i * batchSize;
            int toIndex = Math.min((i + 1) * batchSize, totalSize);
            List<Integer> batch = need.subList(fromIndex, toIndex);
            System.out.println(batch.get(0) + ":" + batch.get(batch.size() - 1));
            runnablePlaywright.startTask(batch.get(0), batch.get(batch.size() - 1));
        }


    }


    @GetMapping("/testAllHistoryData")
    public void testAllHistoryData() {
        List<CompanyEntity> companyEntities = entityService.getAllHistoryData();
        int batchSize = 200;
        int totalSize = companyEntities.size();
        int batchCount = (totalSize + batchSize - 1) / batchSize; // 计算需要的批次

        for (int i = 0; i < batchCount; i++) {
            int fromIndex = i * batchSize;
            int toIndex = Math.min((i + 1) * batchSize, totalSize);
            List<CompanyEntity> batch = companyEntities.subList(fromIndex, toIndex);
            this.runnablePlaywright.startTaskByList(batch, "批次" + i);
        }
    }


    @GetMapping("/exportToExcel")
    public void exportToExcel() {
        ArrayList<CompanyEntity> companyEntities = (ArrayList) entityService.list();
        for (CompanyEntity companyEntity : companyEntities) {
            ArrayList<Director> directors = (ArrayList) directorService.getDirectorByEntity(companyEntity.getId());
            if (ObjectUtil.isNotEmpty(directors)) {
                companyEntity.setDirectors(directors);
            }
        }
        this.runnablePlaywright.saveToExcel(companyEntities, "d:/home/allFile.xls");
    }


}
